<?php
/**
 * Created by PhpStorm.
 * User: bob
 * Date: 2021/10/2
 * Time: 9:14 AM
 */

namespace App\Api\Web\Services;
use App\Api\Account\Models\UserAccountTransaction;
use App\Api\Account\Services\BusinessService;
use App\Api\Admin\Models\Community;
use App\Api\Auth\Models\UsersOpenId;
use App\Api\Web\Models\House;
use App\Api\Web\Models\HouseWanted;
use App\Libs\Wechat\WechatService;

class HouseService
{
    public static function release($params, $from, $userId=0)
    {
        $updates = [];
        $id = data_get($params, 'id', '');
        $name = data_get($params, 'name', '');
        $role = data_get($params, 'role', '');
        $city = data_get($params, 'city', '');
        $cityId = data_get($params, 'city_id', '');
        $tags = data_get($params, 'tag', []);
        $photos = data_get($params, 'photos', []);
        if (is_array($photos) && !empty($photos)) {
            foreach ($photos as $k => $photo) {
                $photos[$k] = $photo;
            }
            $photos = json_encode($photos);
            empty($photos) ? : $updates['photos'] = $photos ;
        }

        $main_img = data_get($params, 'main_img', '');
//        if (!empty($main_img)) {
//            $main_img = env('QINIUYUMING', 'http://cdnrenttest.relianchuanmei.com').'/'.$main_img;
//        }
        $rent_out = data_get($params, 'rent_out', '');
        $community_id = data_get($params, 'community_id', '');
        $bedroom_count = data_get($params, 'bedroom_count', '');
        $toilet_count = data_get($params, 'toilet_count', '');
        $toward = data_get($params, 'toward', '');
        $facilities = data_get($params, 'facilities', '');
        $area = data_get($params, 'area', '');
        $current_floor = data_get($params, 'current_floor', '');
        $all_floor = data_get($params, 'all_floor', '');
        $description = data_get($params, 'description', '');
        $payment_account = data_get($params, 'payment_account', '');
        $totol = data_get($params, 'total', '');
        $mobile = data_get($params, 'mobile', '');
        $wechat = data_get($params, 'wechat', '');
        $qq = data_get($params, 'qq', '');
        $contract = data_get($params, 'contract', '');
        $expiration_date = data_get($params, 'expiration_date', '');
        $is_has_free = data_get($params, 'is_has_free', '');
        $is_check = data_get($params, 'is_check', '');
        $toilet_type = data_get($params, 'toilet_type',  '');
        $bedroom_type = data_get($params, 'bedroom_type', '');
        $bedroom_area = data_get($params, 'bedroom_area', '');
        $sex_limit = data_get($params, 'sex_limit', '');
        $partner_description = data_get($params, 'partner_description',  '');
        $heating_free = data_get($params, 'heating_free', '');
        $deposit = data_get($params, 'deposit', '');
        $siteroom_count = data_get($params, 'sitroom_count', '');
        $stay_date = data_get($params, 'stay_date', '');
        $is_social = data_get($params, 'is_social', '');
        $status = data_get($params, 'status', '');

        if (empty($id)) {
            if (empty($community_id)) {
                throw new \LogicException('请先选择小区', 110);
            }
            $status = 100;
            if ($from == 'admin') {
                $status = '200';
            } else {
                $appId = config('payment.jueqiweilaixcx.wechat.app_id');
                $secret = '';
                if ($appId == config('payment.jueqiweilaixcx.wechat.app_id')) {
                    $secret = config('payment.jueqiweilaixcx.wechat.app_secret');
                }
                $c = new WechatService($appId, $secret);
                $r = $c->msgSecCheck($name);
            }
            $r = \App\Api\Web\Models\House::create([
                'user_id' => $userId,
                'name' => $name,
                'role' => $role,
                'city' => $city,
                'city_id' => $cityId,
                'main_img' => $main_img,
                'photos' => $photos,
                'rent_out' => $rent_out,
                'community_id' => $community_id,
                'bedroom_count' => $bedroom_count,
                'toilet_count' => $toilet_count,
                'toward' => $toward,
                'facilities' => $facilities,
                'area' => $area,
                'current_floor' => $current_floor,
                'all_floor' => $all_floor,
                'description' => $description,
                'payment_account' => $payment_account,
                'total' => $totol,
                'mobile' => $mobile,
                'wechat' => $wechat,
                'qq' => $qq,
                'contract' => $contract,
                'expiration_date' => $expiration_date,
                'is_has_free' => $is_has_free,
                'is_check' => $is_check,
                'toilet_type' => $toilet_type,
                'bedroom_type' => $bedroom_type,
                'bedroom_area' => $bedroom_area,
                'sex_limit' => $sex_limit,
                'partner_description' => $partner_description,
                'heating_free' => $heating_free,
                'deposit' => $deposit,
                'sitroom_count' => $siteroom_count,
                'stay_date' => $stay_date,
                'status' => $status,
                'is_social' => $is_social,
            ]);
            $id = data_get($r, 'id');
            if (is_array($tags) && !empty($tags)) {
                foreach ($tags as $tag) {
                    $isExist = \App\Api\Web\Models\HouseTag::where('house_id', '=', $id)
                        ->where('tag_id', '=', $tag)
                        ->first();
                    if (empty($isExist)) {
                        \App\Api\Web\Models\HouseTag::create([
                            'house_id' => $id,
                            'tag_id' => $tag,
                        ]);
                    }
                }
            }

        } else {

            empty($name) ? : $updates['name'] = $name ;
            empty($role) ? : $updates['role'] = $role ;
            empty($city) ? : $updates['city'] = $city ;
            empty($cityId) ? : $updates['city_id'] = $cityId ;
            empty($rent_out) ? : $updates['rent_out'] = $rent_out ;

            empty($main_img) ? : $updates['main_img'] = $main_img ;
            empty($community_id) ? : $updates['community_id'] = $community_id ;
            empty($bedroom_count) ? : $updates['bedroom_count'] = $bedroom_count ;
            empty($toilet_count) ? : $updates['toilet_count'] = $toilet_count ;
            empty($toward) ? : $updates['toward'] = $toward ;
            empty($facilities) ? : $updates['facilities'] = $facilities ;
            empty($area) ? : $updates['area'] = $area ;
            empty($current_floor) ? : $updates['current_floor'] = $current_floor ;
            empty($all_floor) ? : $updates['all_floor'] = $all_floor ;
            empty($description) ? : $updates['description'] = $description ;
            empty($payment_account) ? : $updates['payment_account'] = $payment_account ;
            empty($totol) ? : $updates['total'] = $totol ;
            empty($mobile) ? : $updates['mobile'] = $mobile ;
            empty($wechat) ? : $updates['wechat'] = $wechat ;
            empty($qq) ? : $updates['qq'] = $qq ;
            empty($contract) ? : $updates['contract'] = $contract;
            empty($expiration_date) ? : $updates['expiration_date'] = $expiration_date;
            empty($is_has_free) ? : $updates['is_has_free'] = $is_has_free;
            empty($is_check) ? : $updates['is_check'] = $is_check;
            empty($toilet_type) ? : $updates['toilet_type'] = $toilet_type;
            empty($bedroom_type) ? : $updates['bedroom_type'] = $bedroom_type;
            empty($bedroom_area) ? : $updates['bedroom_area'] = $bedroom_area;
            empty($sex_limit) ? : $updates['sex_limit'] = $sex_limit;
            empty($partner_description) ? : $updates['partner_description'] = $partner_description;
            empty($heating_free) ? : $updates['heating_free'] = $heating_free;
            empty($deposit) ? : $updates['deposit'] = $deposit;
            empty($siteroom_count) ? : $updates['sitroom_count'] = $siteroom_count;
            empty($stay_date) ? : $updates['stay_date'] = $stay_date;
            empty($status) ? : $updates['status'] = $status;
            empty($is_social) ? : $updates['is_social'] = $is_social;
            if ($userId > 0) {
                $updates['user_id'] = $userId;
            }
            if ($status == '200') {
                $house = House::find($id);
                $userId = data_get($house, 'user_id');
                $isExist = UserAccountTransaction::where('user_id', '=', $userId)
                    ->where('type', '=', '3')
                    ->where('source_id', '=', $id)
                    ->where('source_type', '=', 'house_release')
                    ->first();
                if (!empty($isExist)) {
                    BusinessService::commonIntegralTransaction($userId, 100, $id, 'house_release', '发布房源送积分');
                }
            }
            $r = \App\Api\Web\Models\House::find($id)->update($updates);
            if (is_array($tags) && !empty($tags)) {
                foreach ($tags as $tag) {
                    $isExist = \App\Api\Web\Models\HouseTag::where('house_id', '=', $id)
                        ->where('tag_id', '=', $tag)
                        ->first();
                    if (empty($isExist)) {
                        \App\Api\Web\Models\HouseTag::create([
                            'house_id' => $id,
                            'tag_id' => $tag,
                        ]);
                    }
                }
            }

        }


        return $r;
    }

    public static function houseWantedReleaseOrUpdate($params, $from, $userId=0)
    {
        $id = data_get($params, 'id', '');
        $name = data_get($params, 'name', '');
        $position1_id = data_get($params, 'position1_id', '');
        $position1_name = data_get($params, 'position1_name', '');
        $position1_subway = data_get($params, 'position1_subway', '');
        $position1_subway_site = data_get($params, 'position1_subway_site', '');
        $position1_near = data_get($params, 'position1_near', '');
        $position1_longitude = data_get($params, 'position1_longitude', '');
        $position1_latitude = data_get($params, 'position1_latitude', '');
        $position2_id = data_get($params, 'position2_id', '');
        $position2_name = data_get($params, 'position2_name', '');
        $position2_subway = data_get($params, 'position2_subway', '');
        $position2_subway_site = data_get($params, 'position2_subway_site', '');
        $position2_near = data_get($params, 'position2_near', '');
        $position2_longitude = data_get($params, 'position2_longitude', '');
        $position2_latitude = data_get($params, 'position2_latitude', '');
        $position3_id = data_get($params, 'position3_id', '');
        $position3_name = data_get($params, 'position3_name', '');
        $position3_subway = data_get($params, 'position3_subway', '');
        $position3_subway_site = data_get($params, 'position3_subway_site', '');
        $position3_near = data_get($params, 'position3_near', '');
        $position3_longitude = data_get($params, 'position3_longitude', '');
        $position3_latitude = data_get($params, 'position3_latitude', '');
        $position4_id = data_get($params, 'position4_id', '');
        $position4_name = data_get($params, 'position4_name', '');
        $position4_subway = data_get($params, 'position4_subway', '');
        $position4_subway_site = data_get($params, 'position4_subway_site', '');
        $position4_near = data_get($params, 'position4_near', '');
        $position4_longitude = data_get($params, 'position4_longitude', '');
        $position4_latitude = data_get($params, 'position4_latitude', '');
        $bedroom_count = data_get($params, 'bedroom_count', '');
        $sitroom_count = data_get($params, 'sitroom_count', '');
        $rent_out = data_get($params, 'rent_out', '');
        $bedroom_type = data_get($params, 'bedroom_type', '');
        $toilet_type = data_get($params, 'toilet_type', '');
        $sex_limit = data_get($params, 'sex_limit', '');
        $total_lt = data_get($params, 'total_lt', '');
        $total_gt = data_get($params, 'total_gt', '');
        $description = data_get($params, 'description', '');
        $mobile = data_get($params, 'mobile', '');
        $wechat = data_get($params, 'wechat', '');
        $cityId = data_get($params, 'city_id', '');
        $city = data_get($params, 'city', '');
        $is_social = data_get($params, 'is_social', '');
        $main_img = data_get($params, 'main_img', '');
        $status = data_get($params, 'status', '');

        if (!empty($position1_id)) {
            $community = Community::find($position1_id);
            $city = data_get($community, 'city', '');
            $cityId = data_get($community, 'city_id', '');
            if (!empty($community)) {
                $position1_name = data_get($community, 'name', '');
                $position1_subway = data_get($community, 'subway_id', '');
                $position1_subway_site = data_get($community, 'subway_site_id', '');
                $position1_near = data_get($community, 'near_area', '');
            }
        }
        if (!empty($position2_id)) {
            $community = Community::find($position2_id);
            if (!empty($community)) {
                $position2_name = data_get($community, 'name', '');
                $position2_subway = data_get($community, 'subway_id', '');
                $position2_subway_site = data_get($community, 'subway_site_id', '');
                $position2_near = data_get($community, 'near_area', '');
            }
        }
        if (!empty($position3_id)) {
            $community = Community::find($position3_id);
            if (!empty($community)) {
                $position3_name = data_get($community, 'name', '');
                $position3_subway = data_get($community, 'subway_id', '');
                $position3_subway_site = data_get($community, 'subway_site_id', '');
                $position3_near = data_get($community, 'near_area', '');
            }
        }
        if (!empty($position4_id)) {
            $community = Community::find($position4_id);
            if (!empty($community)) {
                $position4_name = data_get($community, 'name', '');
                $position4_subway = data_get($community, 'subway_id', '');
                $position4_subway_site = data_get($community, 'subway_site_id', '');
                $position4_near = data_get($community, 'near_area', '');
            }
        }
        $positionBriefArr = [];
        if (!empty($position1_name)) {
            $positionBriefArr[] = $position1_name;
        }
        if (!empty($position2_name)) {
            $positionBriefArr[] = $position2_name;
        }
        if (!empty($position3_name)) {
            $positionBriefArr[] = $position3_name;
        }
        if (!empty($position4_name)) {
            $positionBriefArr[] = $position4_name;
        }

        $positionBrief = implode('/', $positionBriefArr);
        if (empty($id)) {
            $status = 100;
            if ($from == 'admin') {
                $status = '200';
            } else {
                $appId = config('payment.jueqiweilaixcx.wechat.app_id');
                $secret = '';
                if ($appId == config('payment.jueqiweilaixcx.wechat.app_id')) {
                    $secret = config('payment.jueqiweilaixcx.wechat.app_secret');
                }
                $c  = new WechatService($appId, $secret);
                $r = $c->msgSecCheck($name);
            }
            $r = HouseWanted::create([
                'name' => $name,
                'position_brief' => $positionBrief,
                'position1_id' => $position1_id,
                'position1_name' => $position1_name,
                'position1_subway' => $position1_subway,
                'position1_subway_site' => $position1_subway_site,
                'position1_near' => $position1_near,
                'position1_longitude' => $position1_longitude,
                'position1_latitude' => $position1_latitude,
                'position2_id' => $position2_id,
                'position2_name' => $position2_name,
                'position2_subway' => $position2_subway,
                'position2_subway_site' => $position2_subway_site,
                'position2_near' => $position2_near,
                'position2_longitude' => $position2_longitude,
                'position2_latitude' => $position2_latitude,
                'position3_id' => $position3_id,
                'position3_name' => $position3_name,
                'position3_subway' => $position3_subway,
                'position3_subway_site' => $position3_subway_site,
                'position3_near' => $position3_near,
                'position3_longitude' => $position3_longitude,
                'position3_latitude' => $position3_latitude,
                'position4_id' => $position4_id,
                'position4_name' => $position4_name,
                'position4_subway' => $position4_subway,
                'position4_subway_site' => $position4_subway_site,
                'position4_near' => $position4_near,
                'position4_longitude' => $position4_longitude,
                'position4_latitude' => $position4_latitude,
                'bedroom_count' => $bedroom_count,
                'sitroom_count' => $sitroom_count,
                'sex_limit' => $sex_limit,
                'rent_out' => $rent_out,
                'bedroom_type' => $bedroom_type,
                'toilet_type' => $toilet_type,
                'total_lt' => $total_lt,
                'total_gt' => $total_gt,
                'description' => $description,
                'mobile' => $mobile,
                'wechat' => $wechat,
                'status' => $status,
                'user_id' => $userId,
                'city' => $city,
                'city_id' => $cityId,
                'is_social' => $is_social,
                'main_img' => $main_img,
            ]);
        } else {
            empty($name) ? : $updates['name'] = $name;
            empty($position1_id) ? : $updates['position1_id'] = $position1_id;
            empty($position1_name) ? : $updates['position1_name'] = $position1_name;
            empty($position1_subway) ? : $updates['position1_subway'] = $position1_subway;
            empty($position1_subway_site) ? : $updates['position1_subway_site'] = $position1_subway_site;
            empty($position1_near) ? : $updates['position1_near'] = $position1_near;
            empty($position1_longitude) ? : $updates['position1_longitude'] = $position1_longitude;
            empty($position1_latitude) ? : $updates['position1_latitude'] = $position1_latitude;
            empty($position2_id) ? : $updates['position2_id'] = $position2_id;
            empty($position2_name) ? : $updates['position2_name'] = $position2_name;
            empty($position2_subway) ? : $updates['position2_subway'] = $position2_subway;
            empty($position2_subway_site) ? : $updates['position2_subway_site'] = $position2_subway_site;
            empty($position2_near) ? : $updates['position2_near'] = $position2_near;
            empty($position2_longitude) ? : $updates['position2_longitude'] = $position2_longitude;
            empty($position2_latitude) ? : $updates['position2_latitude'] = $position2_latitude;
            empty($position3_id) ? : $updates['position3_id'] = $position3_id;
            empty($position3_name) ? : $updates['position3_name'] = $position3_name;
            empty($position3_subway) ? : $updates['position3_subway'] = $position3_subway;
            empty($position3_subway_site) ? : $updates['position3_subway_site'] = $position3_subway_site;
            empty($position3_near) ? : $updates['position3_near'] = $position3_near;
            empty($position3_longitude) ? : $updates['position3_longitude'] = $position3_longitude;
            empty($position3_latitude) ? : $updates['position3_latitude'] = $position3_latitude;
            empty($position4_id) ? : $updates['position4_id'] = $position4_id;
            empty($position4_name) ? : $updates['position4_name'] = $position4_name;
            empty($position4_subway) ? : $updates['position4_subway'] = $position4_subway;
            empty($position4_subway_site) ? : $updates['position4_subway_site'] = $position4_subway_site;
            empty($position4_near) ? : $updates['position4_near'] = $position4_near;
            empty($position4_longitude) ? : $updates['position4_longitude'] = $position4_longitude;
            empty($position4_latitude) ? : $updates['position4_latitude'] = $position4_latitude;
            empty($bedroom_count) ? : $updates['bedroom_count'] = $bedroom_count;
            empty($sitroom_count) ? : $updates['sitroom_count'] = $sitroom_count;
            empty($rent_out) ? : $updates['rent_out'] = $rent_out;
            empty($toilet_type) ? : $updates['toilet_type'] = $toilet_type;
            empty($bedroom_type) ? : $updates['bedroom_type'] = $bedroom_type;
            empty($sex_limit) ? : $updates['sex_limit'] = $sex_limit;
            empty($total_lt) ? : $updates['total_lt'] = $total_lt;
            empty($total_gt) ? : $updates['total_gt'] = $total_gt;
            empty($description) ? : $updates['description'] = $description;
            empty($wechat) ? : $updates['wechat'] = $wechat;
            empty($mobile) ? : $updates['mobile'] = $mobile;
            empty($status) ? : $updates['status'] = $status;
            empty($city) ? : $updates['city'] = $city;
            empty($cityId) ? : $updates['city_id'] = $cityId;
            empty($is_social) ? : $updates['is_social'] = $is_social;
            empty($main_img) ? : $updates['main_img'] = $main_img;
            if ($userId > 0) {
                $updates['user_id'] = $userId;
            }
            if ($status == '200') {
                $house = House::find($id);
                $userId = data_get($house, 'user_id');
                $isExist = UserAccountTransaction::where('user_id', '=', $userId)
                    ->where('type', '=', '3')
                    ->where('source_id', '=', $id)
                    ->where('source_type', '=', 'house_wanted_release')
                    ->first();
                if (!empty($isExist)) {
                    BusinessService::commonIntegralTransaction($userId, 10, $id, 'house_wanted_release', '发布求租送积分');
                }
            }
            $r = 1;
            if (!empty($updates)) {
                HouseWanted::find($id)->update($updates);
            }
        }

        return $r;
    }

    public static function getCityIdByName($city)
    {
        $config = [
            'id' => 1, 'name' => '北京市',
        ];
    }
}